package com.xingchi.tornado.dict.controller;

import com.xingchi.tornado.basic.Result;
import com.xingchi.tornado.dict.enums.DictSourceEnum;
import com.xingchi.tornado.dict.model.dto.DictGroupAddDTO;
import com.xingchi.tornado.dict.model.dto.DictGroupDTO;
import com.xingchi.tornado.dict.model.dto.DictGroupEditDTO;
import com.xingchi.tornado.dict.model.dto.DictGroupQueryDTO;
import com.xingchi.tornado.dict.service.DictGroupService;
import com.xingchi.tornado.model.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *  字典分组 前端控制器
 *
 * @author xingchi
 * @date 2025-05-27 15:24
 */
@RestController
@RequestMapping("/dict/dict-group")
@RequiredArgsConstructor
@Tag(name = "字典分组信息", description = "字典分组操作")
public class DictGroupController {

    private final DictGroupService dictGroupService;

    @PostMapping
    @Operation(summary = "添加字典分组", description = "添加字典分组")
    public Result<Long> addGroup(@RequestBody @Validated DictGroupAddDTO dictGroup) {
        dictGroup.setSource(DictSourceEnum.SYSTEM_ADD.getCode());
        return Result.ok(dictGroupService.addGroup(dictGroup));
    }

    @PutMapping
    @Operation(summary = "修改字典分组信息", description = "修改字典分组信息")
    public Result<Boolean> updateGroup(@RequestBody @Validated DictGroupEditDTO dictGroup) {
        dictGroup.setSource(DictSourceEnum.SYSTEM_ADD.getCode());
        return Result.ok(dictGroupService.updateGroup(dictGroup));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除字典分组", description = "删除字典分组")
    public Result<Boolean> deleteGroup(@PathVariable("id") Long id) {
        return Result.ok(dictGroupService.deleteGroup(id));
    }

    @GetMapping("/pageList")
    @Operation(summary = "分页查询字典分组", description = "分页查询字典分组")
    public Result<PageResult<DictGroupDTO>> pageList(DictGroupQueryDTO query) {
        return Result.ok(dictGroupService.selectPageByQuery(query));
    }

    @GetMapping("/list")
    @Operation(summary = "查询字典分组", description = "查询字典分组")
    public Result<List<DictGroupDTO>> list(DictGroupQueryDTO query) {
        return Result.ok(dictGroupService.selectByQuery(query));
    }
}
